//
//  IGPictureListSortVC.m
//  jingdongfang
//
//  Created by RKL on 2018/5/11.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "IGPictureListSortVC.h"
#import "RTDragCellTableView.h"
#import "RTModel.h"
#import "PictureListSortCell.h"
#import "IGSelectProductFromDrawlistApi.h"
#import "IGAddProductFromDrawlistOrderApi.h"


@interface IGPictureListSortVC ()<RTDragCellTableViewDataSource,RTDragCellTableViewDelegate>

@property (nonatomic, strong) NSArray *data;
/**
 底部tableview
 */
@property (nonatomic , strong) RTDragCellTableView * tableView;

@property (nonatomic , strong) IGSelectProductFromDrawlistApi * drawlistApi;
@property (nonatomic , strong) IGAddProductFromDrawlistOrderApi * drawlistOrderApi;

/**
 记录原始的作品id顺序
 */
@property (nonatomic , copy) NSString * lastDpIdStr;

/**
 改变后的作品id顺序
 */
@property (nonatomic , copy) NSString * changeDpIdStr;
@end

@implementation IGPictureListSortVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"作品排序";
    
    self.view.backgroundColor = RGB(242, 244, 248);
    
    UIBarButtonItem * right = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(sureClick:)];
    [right setTitleTextAttributes:@{NSForegroundColorAttributeName : RGB(0, 171, 230)} forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = right;
    
    self.tableView = [[RTDragCellTableView alloc] init];
    self.tableView.allowsSelection = YES;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.frame = CGRectMake(0, NavigationMagin+5, KSCREENWIDTH, KSCREENHEIGHT-NavigationMagin-5);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self privateGetDate];
}

#pragma mark - 确定按钮点击事件
- (void)sureClick:(UIBarButtonItem *)sender
{
    NSLog(@" 确定按钮点击事件");
    
    NSLog(@"+++++++++++++++++%s++++++++++++++++++++++++",__func__);
    
    int numberOfScections = 1;
    for (int i = 0; i < numberOfScections; i ++)
    {
        NSMutableArray * tempArr = [NSMutableArray array];
        for (int j = 0; j < [_data[i] count] ; j ++)
        {
            IGArtWorkModel * model = _data[i][j];
            [tempArr addObject:model.dpId];
        }
        self.changeDpIdStr = [tempArr componentsJoinedByString:@","];
        self.drawlistOrderApi.dpIds = self.changeDpIdStr;
    }
    
    if (![self.changeDpIdStr isEqualToString:self.lastDpIdStr])//不相同
    {
        __weak typeof(self) weakSelf = self;
        [self.drawlistOrderApi startWithDecodeClass:[IGAddProductFromDrawlistOrderResp class]
                         CompletionBlockWithSuccess:^(id responseObject, NSError *error)
         {
             if (responseObject)
             {
                 IGAddProductFromDrawlistOrderResp * resp = responseObject;
                 if ([resp.resHeader.code isEqualToString:@"success"])
                 {
                     [weakSelf.navigationController popViewControllerAnimated:YES];
                 }
                 else
                 {
                     [weakSelf presentMessageTips:resp.resHeader.message];
                 }
             }
         }];
        
    }
}
#pragma mark - 获取数据
- (void)privateGetDate
{
    __weak typeof(self) weakSelf = self;
    [self.drawlistApi startWithDecodeClass:[IGSelectProductFromDrawlistResp class]
                CompletionBlockWithSuccess:^(id responseObject, NSError *error)
     {
         if (responseObject)
         {
             IGSelectProductFromDrawlistResp * resp = responseObject;
             
             NSMutableArray *arr = [NSMutableArray array];
             
             int numberOfScections = 1;
             for (int i = 0; i < numberOfScections; i ++)
             {
                 NSMutableArray *arr2 = [NSMutableArray array];
                 NSMutableArray * tempArr = [NSMutableArray array];
                 for (int j = 0; j < [resp.data.productList count] ; j ++)
                 {
                     IGArtWorkModel * model = resp.data.productList[j];
                     [arr2 addObject:model];
                     [tempArr addObject:model.dpId];
                 }
                 [arr addObject:arr2];
                 weakSelf.lastDpIdStr = [tempArr componentsJoinedByString:@","];
             }
             _data = arr;
             [weakSelf.tableView reloadData];
         }
     }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.data.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *sec = _data[section];
    return sec.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PictureListSortCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PictureListSortCell"];
    if (!cell)
    {
        cell = [[PictureListSortCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PictureListSortCell"];
    }
    IGArtWorkModel * model = [[IGArtWorkModel alloc] init];
    model = _data[indexPath.section][indexPath.row];
    
    cell.backgroundColor = [UIColor whiteColor];
    cell.authorNameLabel.text = model.seller;
    cell.picNameLabel.text = model.title;
    [cell.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@""]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (NSArray *)originalArrayDataForTableView:(RTDragCellTableView *)tableView
{
    return _data;
}

- (void)tableView:(RTDragCellTableView *)tableView newArrayDataForDataSource:(NSArray *)newArray
{
    _data = newArray;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 76;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


#pragma mark - 懒加载
- (IGSelectProductFromDrawlistApi *)drawlistApi
{
    if (_drawlistApi == nil)
    {
        _drawlistApi = [[IGSelectProductFromDrawlistApi alloc] init];
        _drawlistApi.drawlistId = self.drawlistId;
    }
    return _drawlistApi;
}

- (IGAddProductFromDrawlistOrderApi *)drawlistOrderApi
{
    if (_drawlistOrderApi == nil)
    {
        _drawlistOrderApi = [[IGAddProductFromDrawlistOrderApi alloc] init];
        _drawlistOrderApi.drawlistId = self.drawlistId;
    }
    return _drawlistOrderApi;
}

-(void)dealloc
{
    
}
@end
